package ReentrantLock;

import xianchenglock.S;

import java.util.concurrent.locks.ReentrantLock;

/**
 * @功能
 * @创建时间 2019/12/20
 * ReentrantLock可以指定为公平锁
 */
public class ReentrantLock5 implements Runnable{
    private static ReentrantLock lock = new ReentrantLock(true);// 参数为true表示公平锁，请对比输出结果；
    public void run(){
        for(int i=0; i<100; i++){
            lock.lock();
            try{
                System.out.println(Thread.currentThread().getName()+"获得锁");
            }finally {
                lock.unlock();
            }
        }
    }

    public static void main(String[] args) {
        ReentrantLock5 r1 = new ReentrantLock5();
        Thread th1 = new Thread(r1);
        Thread th2 = new Thread( r1);
        th1.start();
        th2.start();
    }
}
